package com.hzqy.persistence.dao.wchat;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.hzqy.persistence.dao.core.PersistenceDao;
import com.hzqy.web.vo.SysProductVo;
import com.hzqy.web.vo.UserBillVo;
import com.hzqy.web.vo.WchatOrderVo;
import com.hzqy.web.vo.WchatPaymentVo;

@Repository
public class WchatPayDao extends PersistenceDao<WchatOrderVo, Integer>{

	protected final Logger LOG = LoggerFactory.getLogger(WchatPayDao.class);
	
	public SysProductVo selectProductById(int product_id){
		return getSqlSession().selectOne(this.getNamespace() + "selectProductById", product_id);
	}
	
	public int insertOrder(WchatOrderVo wchatOrder){
		return getSqlSession().insert(this.getNamespace() + "insertOrder", wchatOrder);
	}
	
	public int updateOrder(WchatOrderVo wchatOrder){
		return getSqlSession().update(this.getNamespace() + "updateOrder", wchatOrder);
	}
	
	public WchatOrderVo selectOrder(String out_trade_no){
		Map<String, String> params = new HashMap<String, String>();
		params.put("f_wxo_out_trade_no", out_trade_no);
		return getSqlSession().selectOne(this.getNamespace() + "selectOrder", params);
	}
	
	public UserBillVo selectUserBill(String order_id){
		Map<String, String> params = new HashMap<String, String>();
		params.put("f_ub_order_id", order_id);
		return getSqlSession().selectOne(this.getNamespace() + "selectUserBill", params);
	}
	
	public int deleteUnusedWaitingOrder(String serverid){
		Map<String, String> params = new HashMap<String, String>();
		params.put("f_wxo_serverid", serverid);
		return getSqlSession().delete(this.getNamespace() + "deleteUnusedWaitingOrder", params);
	}
	
	public int insertUserBill(UserBillVo userbill){
		return getSqlSession().insert(this.getNamespace() + "insertUserBill", userbill);
	}

	public UserBillVo selectLatestValidUserBill(String serverid, String product_id, String date) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("serverid", serverid);
		params.put("product_id", product_id);
		params.put("date", date);
		return getSqlSession().selectOne(this.getNamespace() + "selectLatestValidUserBill", params);
	}
	
	public int insertApplication(WchatPaymentVo payment){
		return getSqlSession().insert(this.getNamespace() + "insertApplication", payment);
	}
	
	public WchatPaymentVo selectApplicationByTradeNo(String trade_no){
		return getSqlSession().selectOne(this.getNamespace() + "selectApplicationByTradeNo", trade_no);
	}
	
	public int deleteApplicationByTradeNo(String trade_no){
		return getSqlSession().delete(this.getNamespace() + "deleteApplicationByTradeNo", trade_no);
	}
	
	public int deleteEndApplication(){
		return getSqlSession().delete(this.getNamespace() + "deleteEndApplication", String.valueOf(System.currentTimeMillis()));
	}
	
	
}
